/*
 * Copyright (C) 2010 Fred Barrie
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.core;

/**
 * @author fred
 */
public final class ApiResult<E> {

	private final boolean error;
	private final Exception exception;
	private final E result;
	private final String text;

	/**
	 * 
	 */
	public ApiResult() {
		this(false, null, "", null);
	}

	public ApiResult(final ApiResult<?> result) {
		this(result.error, null, result.text, result.exception);
	}

	/**
	 * @param error
	 * @param text
	 * @param exception
	 */
	public ApiResult(final boolean error, final E result, final String text, final Exception exception) {
		this.error = error;
		this.result = result;
		this.text = text;
		this.exception = exception;

	}

	/**
	 * @param error
	 * @param text
	 */
	public ApiResult(final boolean error, final String text) {
		this(error, null, text, null);
	}

	public ApiResult(final E result) {
		this(false, result, "", null);
	}

	/**
	 * @param text
	 */
	public ApiResult(final String text) {
		this(false, null, text, null);
	}

	/**
	 * @param text
	 * @param exception
	 */
	public ApiResult(final String text, final Exception exception) {
		this(true, null, text, exception);
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof ApiResult)) {
			return false;
		}
		final ApiResult<?> other = (ApiResult<?>) obj;
		if (this.error != other.error) {
			return false;
		}
		if (this.result == null) {
			if (other.result != null) {
				return false;
			}
		} else if (!this.result.equals(other.result)) {
			return false;
		}
		if (this.text == null) {
			if (other.text != null) {
				return false;
			}
		} else if (!this.text.equals(other.text)) {
			return false;
		}
		return true;
	}

	/**
	 * @return the exception
	 */
	public Exception getException() {
		return this.exception;
	}

	/**
	 * @return the result
	 */
	public E getResult() {
		return this.result;
	}

	/**
	 * @return the text
	 */
	public String getText() {
		return this.text;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int hash = 1;
		hash = prime * hash + (this.error ? 1231 : 1237);
		hash = prime * hash + (this.result == null ? 0 : this.result.hashCode());
		hash = prime * hash + (this.text == null ? 0 : this.text.hashCode());
		return hash;
	}

	/**
	 * @return the error
	 */
	public boolean isError() {
		return this.error;
	}

	@Override
	public String toString() {
		return "ApiResult [error=" + this.error + ", text=" + this.text + ", exception=" + (this.exception == null ? "" : this.exception.getMessage()) + "]";
	}
}
